<?php
class CompaniesController extends AppController {

    public $name = 'Companies';
    public $helpers = array('Text');

    public function index() {
        $this->Company->recursive = 0;
        $this->set('companies', $this->paginate());
    }

    public function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid Company.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Company->updateClicks($id);
        $this->Company->recursive = 0;
        $this->set('company', $this->Company->read(null, $id));
    }

    public function add() {
        if (!empty($this->data)) {
            $this->Company->create();
            if ($this->Company->save($this->data)) {
                $this->Session->setFlash(__('The Company has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Company could not be saved. Please, try again.', true));
            }
        }
        $companyLocations = $this->Company->CompanyLocation->find('list');
        $companyIndustries = $this->Company->CompanyIndustry->find('list');
        $companyTypes = $this->Company->CompanyType->find('list');
        $this->set(compact('companyLocations', 'companyIndustries', 'companyTypes'));
    }

    public function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Company', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if ($this->Company->save($this->data)) {
                $this->Session->setFlash(__('The Company has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Company could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Company->read(null, $id);
        }
        $companyLocations = $this->Company->CompanyLocation->find('list');
        $companyIndustries = $this->Company->CompanyIndustry->find('list');
        $companyTypes = $this->Company->CompanyType->find('list');
        $this->set(compact('companyLocations','companyIndustries','companyTypes'));
    }

    public function delete($id = null) {
        if (!$id) {
            $this->Response->json(false, __('Invalid id for Company', true));
        }
        if ($this->Company->del($id)) {
            $this->Response->json(true, __('Company deleted', true));
        }
    }

    public function search(){}

}
?>